package com.sais.manage.boot.platform.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.entity.Result;
import com.sais.manage.boot.platform.annotation.Idempotent;
import com.sais.manage.boot.platform.annotation.Throttle;
import com.sais.manage.boot.platform.dto.DictDTO;
import com.sais.manage.boot.platform.dto.DictSaveDTO;
import com.sais.manage.boot.platform.dto.DictSearchDTO;
import com.sais.manage.boot.platform.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 宿舍管理系统-字典 controller 类
 *
 * @author zhang
 * @date 2019/12/2
 **/
@Api(tags = "字典管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/platform/dict")
public class DictController {

    @Resource
    private IDictService dictService;

    @ApiOperation(value = "保存 字典")
    @Idempotent
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Result saveDict(@RequestBody DictSaveDTO dictSaveDTO) {
        dictService.saveDict(dictSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "新建 字典")
  //  @Idempotent
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public Result createDict(@RequestBody DictSaveDTO dictSaveDTO) {
        dictService.createDict(dictSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "删除 字典")
    @Throttle
    @RequestMapping(value = "delete/{dictId}", method = RequestMethod.GET)
    public Result deleteByDictId(@PathVariable(value = "dictId") String dictId) {
        dictService.deleteDict(dictId);
        return Result.response();
    }

    @ApiOperation(value = "更新 字典")
    @Throttle
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result updateDict(@RequestBody DictSaveDTO dictSaveDTO) {
        dictService.updateDict(dictSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "查询 字典")
    @Throttle
    @RequestMapping(value = "select/{dictId}", method = RequestMethod.GET)
    public Result selectByDictCode(@PathVariable(value = "dictId") String dictId) {
        DictDTO dict = dictService.selectDict(dictId);
        return Result.response().data("dict", dict);
    }

    @ApiOperation(value = "查询 字典 列表")
    @Throttle
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result listDict(DictSearchDTO dictSearchDTO,
                           @RequestParam(value = "current", defaultValue = "1") Integer current,
                           @RequestParam(value = "size", defaultValue = "30") Integer size) {
        Page<DictDTO> page = dictService.listDict(dictSearchDTO, current, size);
        return Result.response().data("dicts", page);
    }

}
